<?php
require_once "lib/function/Db.php";
$mysql = Db::getDb();
function isname($name){
    global $mysql;
    $date = $mysql->table("users")->where("username = '$name'")->one();
    return !empty($date);
}
function login($name,$pasw){
    global $mysql;
    $date = $mysql->table("users")->where("username = '$name'")->one();
    if(empty($date) || $date['password'] != md5(md5($pasw).$date['regtime']))
        return false;
    return $date;
}
function register($name,$pasw){
    global $mysql;
    $time = time();
    $pasw = md5(md5($pasw).$time);
    $date = $mysql->table("users")->name("username,password,regtime")->value("'$name','$pasw','$time'")->insert();
    if($date<0)
        return false;
    $mysql->table("info")->name("uid")->value($date)->insert();
    return true;
}
function out(){
    setcookie("token","",time()-1);
}
function dig($lists, $pid = 0)
{
    $t = "";
    foreach ($lists as $value) {
        if ($value['parent'] == $pid) {
            $value['parent'] = dig($lists, $value['id']);
            $t[] = $value;
        }
    }
    return $t;
}

function Navigation(){
    global $mysql;
    $date = $mysql->table("caidan as c join type as t on t.cid = c.id")->name("c.name,t.id,t.parent")->select();
    return $date;
}
function getinfo($id){
    global $mysql;
    return $mysql->table("users as u join info as i on u.id = i.uid")->name("u.*,i.`name`,i.imgurl,i.remarks")->where("i.uid = ".$id)->one();
}
function getlist(){
    global $mysql;
    return $mysql->table("users")->select();
}
function VCode($vcode){
    return md5($vcode) == $_SESSION['verification'];
}
function setinfo($id,$name,$remarks,$imgurl="default.png"){
    global $mysql;
    return $mysql->table("info")->name("name = '$name',imgurl = '$imgurl',remarks = '$remarks'")->where("uid = $id")->update();
}
function delete($id){
    global $mysql;
    $mysql->table("info")->where("uid = $id")->delete();
    return $mysql->table("users")->where("id = $id")->delete();

}